package com.shanling.shanlingcontroller.services.csr;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.support.annotation.IntDef;
import android.support.v4.util.ArrayMap;
import android.util.Log;

import com.qualcomm.qti.libraries.gaia.GAIA;
import com.qualcomm.qti.libraries.gaia.GaiaException;
import com.qualcomm.qti.libraries.gaia.GaiaUtils;
import com.qualcomm.qti.libraries.gaia.packets.GaiaPacket;
import com.qualcomm.qti.libraries.gaia.packets.GaiaPacketBLE;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;

/**
 * Created by ch1 on 2019/12/13.
 */

public class VersionGaiaManager extends AGaiaManager {
    /**
     * To know if we are using the application in the debug mode.
     */
    @SuppressWarnings("unused")
    private static final boolean DEBUG = true;

    /**
     * <p>The listener which implements the MainGaiaManagerListener interface to allow this manager to communicate with a
     * device.</p>
     */
    private final VersionGaiaManagerListener mListener;

    // ====== CONSTRUCTOR ==========================================================================

    /**
     * <p>Main constructor of this class which allows initialisation of a listener to send messages to a device or dispatch
     * any received GAIA messages.</p>
     *
     * @param myListener
     *         An object which implements the {@link MainGaiaManager.MainGaiaManagerListener MyGaiaManagerListener} interface.
     * @param transport
     *          The type of transport this manager should use for the GAIA packet format:
     *          {@link com.qualcomm.qti.libraries.gaia.GAIA.Transport#BLE BLE} or
     *          {@link com.qualcomm.qti.libraries.gaia.GAIA.Transport#BR_EDR BR/EDR}.
     */
    public VersionGaiaManager ( VersionGaiaManagerListener myListener, @GAIA.Transport int transport) {
        super(transport);
        this.mListener = myListener;
    }


    public void getInformation() {
                createRequest(createPacket(GAIA.COMMAND_GET_API_VERSION));
    }

    // ====== PROTECTED METHODS ====================================================================

    @Override // extends GaiaManager
    protected void receiveSuccessfulAcknowledgement(GaiaPacket packet) {

        switch (packet.getCommand()) {
            case GAIA.COMMAND_GET_API_VERSION:
                receivePacketGetAPIVersionACK(packet);
                break;
        }
    }

    @Override
    protected void receiveUnsuccessfulAcknowledgement(GaiaPacket packet) {
        Log.e("weiyu","获取失败");

    }

    @Override
    protected boolean manageReceivedPacket(GaiaPacket packet) {
        return false;
    }

    @Override
    protected void hasNotReceivedAcknowledgementPacket(GaiaPacket packet) {

    }


    @Override // extends GaiaManager
    protected void onSendingFailed(GaiaPacket packet) {
    }



    @Override // extends GaiaManager
    protected boolean sendGAIAPacket(byte[] packet) {
        return mListener.sendGAIAPacket(packet);
    }



    /**
     * <p>Called when this manager handles a packet with the command
     * {@link GAIA#COMMAND_GET_API_VERSION COMMAND_GET_API_VERSION}.</p>
     * <p>This method checks if the packet contains the expected parameters, retrieves them and send them to
     * the listener.</p>
     *
     * @param packet
     *         The received packet with the command COMMAND_GET_API_VERSION.
     */
    private void receivePacketGetAPIVersionACK(GaiaPacket packet) {
        byte[] payload = packet.getPayload();

        final int PAYLOAD_VALUE_1_OFFSET = 1;
        final int PAYLOAD_VALUE_2_OFFSET = PAYLOAD_VALUE_1_OFFSET + 1;
        final int PAYLOAD_VALUE_3_OFFSET = PAYLOAD_VALUE_2_OFFSET + 1;
        final int PAYLOAD_VALUE_LENGTH = 3;
        final int PAYLOAD_MIN_LENGTH = PAYLOAD_VALUE_LENGTH + 1; // ACK status length is 1

        if (payload.length >= PAYLOAD_MIN_LENGTH) {
            mListener.onGetAPIVersion(payload[PAYLOAD_VALUE_1_OFFSET], payload[PAYLOAD_VALUE_2_OFFSET],
                    payload[PAYLOAD_VALUE_3_OFFSET]);
        }
    }



    // ====== INTERFACES ===========================================================================

    /**
     * <p>This interface allows this manager to dispatch messages or events to a listener.</p>
     */
    public interface VersionGaiaManagerListener {

        /**
         * <p>To send over a communication channel the bytes of a GAIA packet using the GAIA protocol.</p>
         *
         * @param packet
         *          The byte array to send to a device.
         * @return
         *          true if the sending could be done.
         */
        boolean sendGAIAPacket(byte[] packet);


        /**
         * <p>This method is called when the device has sent its API version through the acknowledgement of the
         * GAIA command {@link GAIA#COMMAND_GET_API_VERSION COMMAND_GET_API_VERSION}.</p>
         *
         * @param versionPart1
         *          The first number of the version is represented as 1.x.x
         * @param versionPart2
         *          The second number of the version is represented as x.2.x
         * @param versionPart3
         *          The third number of the version is represented as x.x.3
         */
        void onGetAPIVersion(int versionPart1, int versionPart2, int versionPart3);
    }

}
